//
//  WeatherCardView.swift
//  lovelyWeather
//
//  Created by MAC on 2019/7/30.
//  Copyright © 2019 zk. All rights reserved.
//

import UIKit

let kScreenW: CGFloat = UIScreen.main.bounds.width
let kScreenH: CGFloat = UIScreen.main.bounds.height

class WeatherCardView: UIView {

    var data: infoModel? {
        didSet {
            guard let data = data else {
                return
            }
            
            time.text = data.day
            temperature.text = data.tem
            status.text = data.wea
            wind.text = data.win
            humidity.text = data.win_speed
            
            if data.wea.contains("晴"){
                sun()
            }
            if data.wea.contains("云"){
                weatherStatusLayer.addSublayer(sunLayer())
                weatherStatusLayer.addSublayer(cloudLayer())
            }else if data.wea.contains("阴"){
                 weatherStatusLayer.addSublayer(cloudLayer())
            }
           
            if data.wea.contains("雨"){
                rain()
            }else if data.wea.contains("雪"){
                
            }
            if data.wea.contains("雷"){
                alighting()
            }
            
        }
    }
    let colors = [color1,color2,color3,color4]
    let bgColor: CGColor = UIColor.white.cgColor
    var lineColor: CGColor = UIColor.orange.cgColor
    var lightingColor: CGColor = UIColor.orange.cgColor
    var weatherStatusLayer: CALayer!
    
    lazy var time: UILabel = {
         let lab = UILabel()
        lab.font = UIFont.boldSystemFont(ofSize: 16)
        lab.text = "8点"
        lab.textColor = UIColor.white
        return lab
    }()
    
    lazy var temperature: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 30)
        lab.text = "-1º"
        lab.textColor = UIColor.white
        return lab
    }()
    
    lazy var humidity: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 16)
        lab.text = "Humidity: 88%"
        lab.textColor = UIColor.white
        return lab
    }()
    
    lazy var wind: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 16)
        lab.text = "Wind: W 1 mph"
        lab.textColor = UIColor.white
        return lab
    }()
    
    lazy var status: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 16)
        lab.text = "Mostly Sunny"
        lab.textColor = UIColor.white
        return lab
    }()
    
    init(frame: CGRect, bgColor: UIColor,lineColor: UIColor) {
        super.init(frame: frame)
        self.backgroundColor = bgColor
        self.lineColor = lineColor.cgColor
        self.lightingColor = randomColor(aColor: lineColor).cgColor
        
         setupInitialData()
       
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
         setupInitialData()
    }
    
    func randomColor(aColor: UIColor) -> UIColor {
        let randomIndex = arc4random_uniform(4) % 4
        
        var lineColor = colors[Int(randomIndex)]
        
        if lineColor == aColor {
            lineColor = colors[Int(randomIndex + 1) % 4]
        }
        return lineColor
    }
    
    func sun() {
        
        weatherStatusLayer.addSublayer(sunLayer())
    }
    
    func rain() {
        
        weatherStatusLayer.addSublayer(cloudLayer())
        
        
        let  arain = rainLayer()
        
        weatherStatusLayer.addSublayer(arain)
        weatherStatusLayer.addSublayer(rainLayer())
        let rotationAnim = CAKeyframeAnimation(keyPath: "transform.translation.y")
        rotationAnim.keyTimes = [0, 1]
        rotationAnim.values = [0, 45]
        rotationAnim.repeatCount = MAXFLOAT
        rotationAnim.duration = 0.6
        rotationAnim.isRemovedOnCompletion = false
        arain.add(rotationAnim, forKey: nil)
        
    }
    
    func alighting() {
        
      weatherStatusLayer.addSublayer(cloudLayer())
      weatherStatusLayer.addSublayer(lighting())
    }
    

    func setupInitialData() {
        self.addSubview(time)
        self.addSubview(temperature)
        self.addSubview(status)
        self.addSubview(wind)
        self.addSubview(humidity)
        
        time.frame = CGRect(x: kScreenW / 2 + 40, y: 25, width: kScreenW / 2 - 40, height: 21)
        temperature.frame = CGRect(x: time.frame.minX, y: time.frame.maxY, width: time.frame.width, height: 35)
        status.frame = CGRect(x: time.frame.minX, y: temperature.frame.maxY + 20, width: time.frame.width, height: 18)
        wind.frame = CGRect(x: time.frame.minX, y: status.frame.maxY + 10, width: time.frame.width, height: 18)
        humidity.frame = CGRect(x: time.frame.minX, y: wind.frame.maxY + 10, width: time.frame.width, height: 18)
        
        
        weatherStatusLayer = CALayer()
        weatherStatusLayer.frame = CGRect(x: 20, y: 125, width: 120, height: 120)
        self.layer.addSublayer(weatherStatusLayer)
        self.layer.masksToBounds = true
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    
    
    func sunLayer() -> CALayer {
        
        let bgColor = UIColor.clear.cgColor
        let lineWidth: CGFloat = 8
        let sun = CALayer()
        let radius: CGFloat = 25
        let center: CGFloat = 60
        sun.frame = CGRect(x: 0, y: 0, width: 120, height:  120)
        
        
        let circlePath = UIBezierPath.init(arcCenter: CGPoint(x: center, y: center), radius: radius, startAngle: 0, endAngle: 2 * CGFloat.pi, clockwise: false);
        
        let circleLayer = CAShapeLayer()
        circleLayer.path = circlePath.cgPath
        circleLayer.fillColor = bgColor
        circleLayer.lineWidth = lineWidth
        circleLayer.strokeColor = lineColor
        sun.addSublayer(circleLayer)
        
        let x: CGFloat = center
        let y: CGFloat = center
        let dx: CGFloat = radius + 15
        
        
        for idx in 0...11 {
            let col = CGFloat(idx) * (CGFloat.pi / 6)
            let yy = CAShapeLayer()
            let newx = x + dx * cos(col)
            let newy = y - dx * sin(col)
            
            var length: CGFloat = 3
            if (idx + 1) % 2 == 0 {
                length = 5
            }
            
            let newx1 = x + (dx + length) * cos(col)
            let newy1 = y - (dx + length) * sin(col)
            
            let path =  UIBezierPath()
            path.move(to: CGPoint(x: newx, y: newy))
            path.addLine(to: CGPoint(x: newx1, y: newy1))
            
            yy.path = path.cgPath
            yy.lineCap = .round
            yy.strokeColor = lineColor
            yy.fillColor = lineColor
            yy.lineWidth = lineWidth
            sun.addSublayer(yy)
        }
        
        let rotationAnim = CAKeyframeAnimation(keyPath: "transform.rotation.z")
        rotationAnim.keyTimes = [0, 1]
        rotationAnim.values = [0, CGFloat(Double.pi) * 2]
        rotationAnim.repeatCount = MAXFLOAT
        rotationAnim.duration = 4
        rotationAnim.isRemovedOnCompletion = false
        sun.add(rotationAnim, forKey: nil)
        
        return sun
    }
    
    func cloudLayer() -> CALayer {
        let cloud = CALayer()
        
        let center1 = CGPoint(x: 40, y: 100)
        
        let path1 = UIBezierPath(arcCenter: center1, radius: 20, startAngle: -CGFloat.pi / 2, endAngle: CGFloat.pi / 2, clockwise:false)
        
        let center2 = CGPoint(x: 40 + 10*sin(CGFloat.pi/4), y: 80 - 10*sin(CGFloat.pi/4))
        
        path1.addArc(withCenter: center2, radius: 10, startAngle: CGFloat.pi / 2 + CGFloat.pi / 4, endAngle:CGFloat.pi * 3 / 2 + CGFloat.pi / 4, clockwise: true)
        
        let newa = asin(20*sin(CGFloat.pi/4) / 80)
        
        let c2dx = 10 * sin(CGFloat.pi/4)
        
        let radius3: CGFloat = 30
        let c3X = sqrt(radius3 * radius3 - 2*c2dx*2*c2dx)
        let center3 = CGPoint(x: c3X + 40 + 2*c2dx, y: 80 - 10*sin(CGFloat.pi/4))
        
        path1.addArc(withCenter: center3, radius: radius3, startAngle: CGFloat.pi + newa, endAngle:newa, clockwise: true)
        let center4 = CGPoint(x: 2*c3X + 40 + 2*c2dx, y: 100)
        path1.addArc(withCenter: center4, radius: 20, startAngle: -CGFloat.pi / 2, endAngle: CGFloat.pi/2, clockwise: true)
        
        path1.addLine(to: CGPoint(x: 40, y: 120))
        let circle1 = CAShapeLayer()
        circle1.path = path1.cgPath
        circle1.fillColor = lineColor
        cloud.addSublayer(circle1)
        
        return cloud
        
    }
    
    func rainLayer() -> CALayer{
        
        let rain = CALayer()
        
        
        let drop1 = rainDrop()
        drop1.frame = CGRect(x: 35, y: 0, width: 26, height: 25)
        
        let drop2 = rainDrop()
        drop2.frame = CGRect(x: 60, y: 0, width: 26, height: 25)
        
        
        let drop3 = rainDrop()
        drop3.frame = CGRect(x: 85, y: 0, width: 26, height: 25)
        
        rain.addSublayer(drop1)
        
        rain.addSublayer(drop2)
        
        rain.addSublayer(drop3)
        rain.frame = CGRect(x: 0, y: 120, width: 120, height: 180)
        return rain
    }
    
    func rainDrop() -> CAShapeLayer {
        let path1 = UIBezierPath()
        let rain1x: CGFloat = 13
        let rainRadius: CGFloat = 13
        let rainHeight: CGFloat = 25
        let rainXControl: CGFloat = 23
        path1.move(to: CGPoint(x: rain1x, y: 0))
        path1.addQuadCurve(to: CGPoint(x: rain1x, y: rainHeight), controlPoint: CGPoint(x: rain1x - rainRadius, y: rainXControl))
        path1.addQuadCurve(to: CGPoint(x: rain1x, y: 0), controlPoint: CGPoint(x: rain1x + rainRadius, y: rainXControl))
        
        let Drop = CAShapeLayer()
        Drop.path = path1.cgPath
        Drop.fillColor = lineColor
        return Drop
    }
    
    func lighting() -> CALayer{
        let lighting = CALayer()
        
        let path = UIBezierPath()
        let centerX: CGFloat = 72.5
        let width: CGFloat = 28
        let p1 = CGPoint(x: centerX, y: 50)
        path.move(to: p1)
        
        let p2 = CGPoint(x: centerX - width / 2, y: 90)
        path.addLine(to: p2)
        
        let p3 = CGPoint(x: centerX, y: 87)
        path.addLine(to: p3)
        
        let p4 = CGPoint(x: centerX, y: 120  )
        path.addLine(to: p4)
        
        let p5 = CGPoint(x: centerX + width / 2, y: 80)
        path.addLine(to: p5)
        
        let p6 = CGPoint(x: centerX, y: 83)
        path.addLine(to: p6)
        
        path.close()
        
        
        let layer = CAShapeLayer()
        layer.path = path.cgPath
        layer.fillColor = lightingColor
        lighting.addSublayer(layer)
        
        return lighting
    }
}
